package com.hzit.portal.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.hzit.common.utils.R;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类名：
 * 作者：WF
 * 功能：对限流出现异常时的自定义内容的显示,如果配置了/block-page页面，此时，下面的优先处理级别更高！
 */
@Configuration
public class SentinelConfig implements BlockExceptionHandler {
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
		response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
		R error = R.error(500, "出现了限流异常！");
		response.getWriter().println(JSON.toJSONString(error));
		response.getWriter().flush();
	}
}
